package cn.tedu.mall.common.restful;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.domain.Pageable;

import java.io.Serializable;
import java.util.List;

@Data
public class JsonPage<T> implements Serializable {
    //定义实际需求需要使用的分页信息
    @ApiModelProperty(value = "总页数",name = "totalPages")
    private Integer totalPages; //总页数
    @ApiModelProperty(value = "总条数",name = "totalCount")
    private Long totalCount; //总条数
    @ApiModelProperty(value = "页码",name = "pageNum",example = "1")
    private Integer pageNum; //页码
    @ApiModelProperty(value = "每页大小",name = "pageSize",example = "10")
    private Integer pageSize; //每页大小
    //如果还需要其他属性，可以继续添加

    //分页数据
    @ApiModelProperty(value = "分页数据",name = "data")
    private List<T> data;

    //将PageHelper中的属性，赋值给自定义的分页信息以及分页数据
    public static <T> JsonPage<T> restPage(PageInfo<T> pageInfo){
        JsonPage<T> jsonPage = new JsonPage<>();
        //赋值分页信息
        jsonPage.setTotalPages(pageInfo.getPages());
        jsonPage.setTotalCount(pageInfo.getTotal());
        jsonPage.setPageNum(pageInfo.getPageNum());
        jsonPage.setPageSize(pageInfo.getPageSize());
        //分页数据
        jsonPage.setData(pageInfo.getList());

        //返回
        return jsonPage;
    }
}
